/**
*      author: Y_Yao
*      created: 2024-12-16 16:52:07
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 1e18;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 7;
int n,k;
int p[N];
vector<int> v(N);

int pow(int a,int b,int mod)
{
    int ret = 1;
    while(b)
    {
        if(b & 1) ret = ret * a % mod;
        a = a * a % mod;
        b = b >> 1;
    }
    return ret;
}

void solve()
{
    cin >> n >> k;
    for(int i = 1;i <= n;i++)
    {
        cin >> p[i];
    }

    for(int i = 1;i <= n;i++)
    {
        if(v[i] != 0) continue;
        vector<int> cycle; 
        int cur = i;
        do
        {
            cycle.push_back(cur);
            cur = p[cur];
        } while (cur != i);

        int step = pow(2,k,cycle.size());
        
        for(int i = 0;i < cycle.size();i++)
        {
            v[cycle[i]] = cycle[(i + step) % cycle.size()];
        }
    }

    for(int i = 1;i <= n;i++)
    {
        cout << v[i] << " ";
    }
    cout << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}